//vite.config.ts
import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import path from "path"
import UnoCSS from "unocss/vite"
import Icons from "unplugin-icons/vite"
import IconsResolver from "unplugin-icons/resolver"
import AutoImport from "unplugin-auto-import/vite"
import Components from "unplugin-vue-components/vite"
import { ElementPlusResolver } from "unplugin-vue-components/resolvers"
import ElementPlus from "unplugin-element-plus/vite"
// https://vite.dev/config/
export default defineConfig({
  resolve: {
    alias: [{ find: "@", replacement: path.resolve(__dirname, "src") }]
  },
  server: {
    port: 8080,
    open: true,
    proxy: {
      "/api": {
        target: process.env.VITE_APP_BASE_API, // 使用环境变量
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, "")
      }
    }
  },
  plugins: [
    vue(),
    UnoCSS({
      configFile: "./UnoCSS.config.ts"
    }),
    AutoImport({
      // api
      imports: ["vue", "vue-router", "pinia"],
      resolvers: [
        ElementPlusResolver(),
        IconsResolver({
          prefix: "Icon"
        })
      ],
      eslintrc: { enabled: false } // 给eslint生产的配置 只需要一次
    }),
    Components({
      //解析Element Plus组件
      resolvers: [
        ElementPlusResolver(),
        IconsResolver({
          // 自动注册图标组件
          enabledCollections: ["ep"]
        })
      ],
      //所有的自定义组件可以自动加载
      dirs: [
        "src/components",
        "src/layout/components",
        "src/views/**/components",
        "src/views/"
      ]
    }),
    ElementPlus({}), // 导入样式 不需要引入
    Icons({
      autoInstall: true
    })
  ]
})
